package com.loveyukari.service;


import com.loveyukari.common.exception.InternalException;
import com.loveyukari.dao.ExchangeGfitDao;
import com.loveyukari.dao.ExchangeHistoryDao;
import com.loveyukari.model.Auth;
import com.loveyukari.model.ExchangeGfit;
import com.loveyukari.model.ExchangeHistory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExchangeService {

    @Autowired
    private ExchangeGfitDao exchangeGfitDao;
    @Autowired
    private ExchangeHistoryDao exchangeHistoryDao;
    @Autowired
    private AuthService authService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private RedisService redisService;

    private String exchangeGfitKey = "exchange:gfit:";


    public List<ExchangeGfit> getGfitAll(){
        return exchangeGfitDao.selectAll();
    }

    public List<ExchangeGfit> getGfitOnline(){
        return exchangeGfitDao.selectOnline();
    }

    public ExchangeGfit getGfitDetail(Integer id){
        if (id == null){
            return null;
        }
        String key = exchangeGfitKey + id;
        ExchangeGfit exchangeGfit = redisService.get(key, ExchangeGfit.class);
        if (exchangeGfit == null){
            exchangeGfit = exchangeGfitDao.selectById(id);
            redisService.set(key,exchangeGfit);
        }
        return exchangeGfit;
    }

    public void reloadGfit(Integer id){
        if (id == null){
            return;
        }
        redisService.del(exchangeGfitKey+id);
    }

    public void addGfit(ExchangeGfit exchangeGfit){
        exchangeGfitDao.insert(exchangeGfit);
    }

    public void updateGfit(ExchangeGfit exchangeGfit){
        exchangeGfitDao.update(exchangeGfit);
        this.reloadGfit(exchangeGfit.getId());
    }

    public void updateGfitStatus(int gfitId){
        ExchangeGfit exchangeGfit = exchangeGfitDao.selectById(gfitId);
        if (exchangeGfit != null){
            exchangeGfitDao.updateStatus(exchangeGfit.getId(),exchangeGfit.getStatus() == 0 ? 1 : 0);
        }
    }

    public void addHistory(ExchangeHistory exchangeHistory){
        exchangeHistoryDao.insert(exchangeHistory);
    }
    public List<ExchangeHistory> getHistoryAll(){
        List<ExchangeHistory> list = exchangeHistoryDao.selectAll();
        for (ExchangeHistory exchangeHistory :
                list) {
            exchangeHistory.setAddress(addressService.getCacheAddress(exchangeHistory.getAddressId()));
            exchangeHistory.setAuth(authService.getCacheAuth(exchangeHistory.getAuthId()));
            exchangeHistory.setExchangeGfit(this.getGfitDetail(exchangeHistory.getGfitId()));
        }
        return list;
    }

    public List<ExchangeHistory> getAuthHistory(int authId){
        List<ExchangeHistory> list = exchangeHistoryDao.selectByAuth(authId);
        for (ExchangeHistory exchangeHistory :
                list) {
            exchangeHistory.setAddress(addressService.getCacheAddress(exchangeHistory.getAddressId()));
            exchangeHistory.setAuth(authService.getCacheAuth(exchangeHistory.getAuthId()));
            exchangeHistory.setExchangeGfit(this.getGfitDetail(exchangeHistory.getGfitId()));
        }
        return list;
    }

    public int getAuthPrice(int authId){
        return exchangeHistoryDao.sumAuthPrice(authId);
    }

    public void authExchangeGfit(int authId,int gfitId) throws InternalException{
        ExchangeGfit exchangeGfit = exchangeGfitDao.selectById(gfitId);
        if (exchangeGfit == null){
            throw new InternalException("兑换物品不存在");
        }
        if (exchangeGfit.getNum() <= 0) {
            throw new InternalException("兑换物品库存不足");
        }
        Auth auth = authService.getCacheAuth(authId);
        if (auth == null){
            throw new InternalException("用户不存在");
        }
        int price = exchangeHistoryDao.sumAuthPrice(authId);
        if (price + exchangeGfit.getPrice() > auth.getScore()){
            throw new InternalException("积分不足");
        }
        exchangeGfit.setNum(exchangeGfit.getNum()-1);
        exchangeGfitDao.update(exchangeGfit);
        this.reloadGfit(exchangeGfit.getId());
        ExchangeHistory exchangeHistory = new ExchangeHistory();
        exchangeHistory.setAuthId(authId);
        exchangeHistory.setPrice(exchangeGfit.getPrice());
        exchangeHistory.setGfitId(exchangeGfit.getId());
        exchangeHistoryDao.insert(exchangeHistory);
    }


    public void updateAddress(Integer id,Integer addressId,Integer authId){
        if (addressId == null || authId == null || id == null){
            return;
        }
        ExchangeHistory exchangeHistory = exchangeHistoryDao.selectById(id);
        if (authId.equals(exchangeHistory.getAuthId())){
            exchangeHistoryDao.updateAddress(id,addressId);
        }
    }

    public void updateExpress(Integer id,String expressNo,String expressName) throws InternalException{
        if (StringUtils.isBlank(expressName)){
            throw new InternalException("请填写快递名");
        }
        if (StringUtils.isBlank(expressNo)){
            throw new InternalException("请填写快递单号");
        }
        ExchangeHistory exchangeHistory = exchangeHistoryDao.selectById(id);
        if (exchangeHistory.getAddressId() == 0){
            throw new InternalException("未选择地址");
        }
        exchangeHistoryDao.updateExpress(id,expressNo,expressName);
    }

    public void updateSuccess(Integer id){
        exchangeHistoryDao.updateStatus(id,2);
    }

    public ExchangeHistory getExchangeHistoryById(Integer id){
        ExchangeHistory exchangeHistory = exchangeHistoryDao.selectById(id);
        return exchangeHistory;
    }


}
